﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.SysMgr;

namespace Sunimage.Spjg.DAL.SysMgr
{
    public class BulletinDa
    {
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        public static void Insert(Bulletin bulletin)
        {
            Insert(bulletin, DBHelper.GetDatabase(), null);
        }
        public static void Insert(Bulletin bulletin, TranscationHelper trans)
        {
            Insert(bulletin, trans.DataBase, trans.Transaction);
        }
        public static void Insert(Bulletin bulletin, Database db, DbTransaction trans)
        {
            object[] pars = new object[]
            {
                bulletin.CreateTime,
                bulletin.Title,
                bulletin.Content
            };
            try
            {
                if (trans == null)
                    bulletin.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertBulletin", pars));
                else
                    bulletin.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertBulletin", pars));

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(int id)
        {
            Delete(id, DBHelper.GetDatabase(), null);
        }
        public static void Delete(int id, TranscationHelper trans)
        {
            Delete(id, trans.DataBase, trans.Transaction);
        }
        public static void Delete(int id, Database db, DbTransaction trans)
        {
            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_DeleteBulletin", id);
                else
                    db.ExecuteNonQuery(trans, "Usp_DeleteBulletin", id);

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, true);
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user"></param>
        public static void Update(Bulletin bulletin)
        {
            Update(bulletin, DBHelper.GetDatabase(), null);
        }
        public static void Update(Bulletin bulletin, TranscationHelper trans)
        {
            Update(bulletin, trans.DataBase, trans.Transaction);
        }
        public static void Update(Bulletin bulletin, Database db, DbTransaction trans)
        {
            object[] pars = new object[]
            {
                bulletin.Id,
                bulletin.CreateTime,
                bulletin.Title,
                bulletin.Content
            };

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_UpdateBulletin", pars);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateBulletin", pars);

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        /// <summary>
        /// 获取所有数据用户信息
        /// </summary>
        /// <returns></returns>
        public static List<Bulletin> GetAll()
        {
            return GetAll(DBHelper.GetDatabase(), null);

        }
        public static List<Bulletin> GetAll(TranscationHelper trans)
        {
            return GetAll(trans.DataBase, trans.Transaction);
        }
        public static List<Bulletin> GetAll(Database db, DbTransaction trans)
        {
            List<Bulletin> gsjUsers = new List<Bulletin>();
            IDataReader dataReader = null;
            if (trans == null)
                dataReader = db.ExecuteReader("Usp_GetBulletinList");
            else
                dataReader = db.ExecuteReader(trans, "Usp_GetBulletinList");

            using (dataReader)
            {
                while (dataReader.Read())
                {
                    gsjUsers.Add(PopulateUser(dataReader));
                }
            }

            return gsjUsers;
        }

        //辅助方法
        private static Bulletin PopulateUser(IDataReader dataReader)
        {
            Bulletin bulletin = new Bulletin();
            bulletin.Id = dataReader.GetInt32(0);
            bulletin.CreateTime = dataReader.GetDateTime(1);
            bulletin.Title = dataReader.GetString(2);
            bulletin.Content = dataReader.GetString(3);

            return bulletin;
        }
    }
}
